package com.ikan.positionserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ikan.positionserver.entity.RackColumn;
import com.ikan.positionserver.response.JsonReturn;
import com.ikan.positionserver.service.IRackColumnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * rackColumn前端控制器  RestController注解 将结果以JSON形式返回
 * </p>
 *
 * @author ikan
 * @since 2024-04-10
 */
@Slf4j
@RestController
@RequestMapping("/rackColumn")
@Api(value = "RackColumn", tags = "RackColumn")
public class RackColumnController {
    @Autowired
    private IRackColumnService rackColumnService;

    /**
     * POST请求方式
     *
     * @param rackColumn 保存的对象
     * @return JsonReturn
     */
    @PostMapping
    @ApiOperation(value = "插入记录")
    public JsonReturn insert(RackColumn rackColumn) {
        try {
            return JsonReturn.buildSuccess(rackColumnService.save(rackColumn) ? rackColumn :
                    "rackColumn--新增失败");
        } catch (Exception e) {
            log.error("新增失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }

    }

    /**
     * PUT请求方式
     *
     * @param rackColumn 要修改的对象
     * @return JsonReturn
     */
    @PutMapping
    @ApiOperation(value = "修改记录")
    public JsonReturn update(RackColumn rackColumn) {
        try {
            return JsonReturn.buildSuccess(rackColumnService.updateById(rackColumn) ? rackColumn :
                    "rackColumn--修改失败");
        } catch (Exception e) {
            log.error("修改失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }

    }

    /**
     * 批量删除 DELETE请求方式
     *
     * @param ids Long 类型 List 集合
     * @return JsonReturn
     */
    @DeleteMapping("remove")
    @ApiOperation(value = "删除多条记录")
    public JsonReturn delete(@RequestBody List<Long> ids) {
        try {
            rackColumnService.removeByIds(ids);
            return JsonReturn.buildSuccess(ids, "操作成功");
        } catch (Exception e) {
            log.error("操作失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }
    }

    /**
     * 查询一个 GET请求
     *
     * @param id 查找对象的主键ID
     * @return JsonReturn
     */
    @GetMapping("findOne/{id}")
    @ApiOperation(value = "查询单条记录")
    public JsonReturn findOne(@PathVariable("id") Long id) {
        try {
            RackColumn rackColumn = rackColumnService.getById(id);
            return JsonReturn.buildSuccess(rackColumn, "操作成功");
        } catch (Exception e) {
            log.error("查询单个失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }
    }

    /**
     * 查询所有 GET
     *
     * @param pageNum  当前页
     * @param pageSize 每页最大数据数
     * @return JsonReturn
     */
    @GetMapping("findAll/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页查询")
    public JsonReturn findAll(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        try {
            Page<RackColumn> page = rackColumnService.page(new Page<RackColumn>(pageNum, pageSize));
            return JsonReturn.buildSuccess(page, "操作成功");
        } catch (Exception e) {
            log.error("分页查询失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }
    }

}
